
package com.yoga.common.appexception;


/**
 * Created by chenjin on 2020/2/19.
 */
public class AppException extends  RuntimeException{

    public AppException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
        super(message, cause, enableSuppression, writableStackTrace);
    }

    private Integer code;

    private String message;

    private AppErrorCodeEnum responseCode;

    public AppException(AppErrorCodeEnum responseCode){
        super(responseCode.getMessage());
        this.code = responseCode.getCode();
        this.message = responseCode.getMessage();
        this.responseCode = responseCode;
    }

    public AppException(Integer code,String message){
        super(message);
        this.code=code;
        this.message=message;
        this.responseCode=AppErrorCodeEnum.CUSTOM_ERROR;
    }

    public AppException(AppErrorCodeEnum responseCode, String message){
        super(message);
        this.responseCode = responseCode;
        this.code = responseCode.getCode();
        this.message=message;
    }

    public AppException(AppErrorCodeEnum responseCode, Throwable cause){
        super(cause);
        this.responseCode = responseCode;
    }

    public AppErrorCodeEnum getResponseCode() {
        return responseCode;
    }

    public Integer getCode() {
        if (responseCode!=null){
            code=responseCode.getCode();
        }
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    @Override
    public String getMessage() {
        if (message!=null){
            return message;
        }
        if (responseCode!=null){
            message=responseCode.getMessage();
        }
        return message;
    }

}
